For Statement

Iteration is a key to success in programming. Perhaps, it's the very reason the computer was even concepted. Most are familiar with the uses of a for-statement, and nothing changes semantically in Python. Syntax differs, but use cases are the same. Let's take a look!

The for statement is used for iteration. We can do the same task multiple times with a single for statement. There are many varying syntaxes, and we will look at all of them. Let's look at the basics:

for item in domain:
    // code

For loops typically iterate over elements in a domain. These elements can be any type as item takes on the value of each consecutive value of the domain. A domain can be a data structure like a list, tuple, dictionary, etc., it could be a range of numbers. In specific, it is anything that is of type Iterable. Let's look at a few examples of using the for statement.

Example 1 A natural example is to assume that we have a list of objects. Let's say we have a list of names, and we want to print all of them. We could seperately access each one.. that's too slow.. we put 2 and 2 together and decide to use a for statement.


In [26]:
names = ["Harry", "Joanna", "Micah", "Robertson"]

for name in names:
    print(name)


Harry
Joanna
Micah
Robertson

Let's think as to how it worked. We provided a list of 4 names. We specified the syntax as

for item in domain:
    print(item)

where our item was a name, and domain was the list of names. We printed the name, and each time the loop ran it went to the next name until it hit "Robertson" and ran out of names to go to.

Example 2 We can emulate the process with a tuple, too. Let's test it out. Here, we've inserted a movie name, the cost to go to the movie at the time, and the box office earnings in the first 24 hours.


In [27]:
my_tup = ("Rocky", 19.0, 3400403)
for value in my_tup:
    print(value, end=" ")


Rocky 19.0 3400403 

We iterated over the 3 values (movie name, cost, box office) by letting our variable "value" take on the identity of each object, and then get printed without making a newline character ("\n").

Example 3 Iterating over dictionaries works, too. Let's print the stock count for a few employees at a startup.


In [28]:
stock_dict = {"Micah":20, "Loanthony":31, "Fofo":12}

In [29]:
for person in stock_dict:
    print("Person: {x}   Stocks: {y}".format(x=person,y=stock_dict[person]))


Person: Loanthony   Stocks: 31
Person: Micah   Stocks: 20
Person: Fofo   Stocks: 12

Python has a range() function. Range simply allows us to specify a number range like this:

range(inclusive, exclusive)

Where, in a for loop, the item variable takes on every value from the incusive up until the exclusive number.


In [31]:
# Ranges start from 0 at default
for num in range(8):
    odd_or_even = None
    if num % 2 == 0:
        odd_or_even = "Even"
    else:
        odd_or_even = "Odd"
    print("{x} is {y}".format(x=num, y=odd_or_even))


0 is Even
1 is Odd
2 is Even
3 is Odd
4 is Even
5 is Odd
6 is Even
7 is Odd